/*
 * Copyright 2007-2008  Lukasz Laszko lukaszlaszko@gmail.com
 * 
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and this
 * permission notice and warranty disclaimer appear in supporting
 * documentation, and that the name of the author not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * 
 * The author disclaim all warranties with regard to this
 * software, including all implied warranties of merchantability
 * and fitness.  In no event shall the author be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether
 * in an action of contract, negligence or other tortious action,
 * arising out of or in connection with the use or performance of
 * this software. 
 */
package org.pg.biomedics.asr.data;

import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.File;
import java.io.Reader;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;

/**
 * Base class for all data managers included in data package
 * @author Lukasz Laszko
 */
public abstract class BaseDataManager
{
    // <editor-fold desc="Constants">
    
    private static final String SQL_MAP_CONFIG_KEY = "ibatis.sqlmapconfig";
    
    // </editor-fold>
    
    // <editor-fold desc="Fields">
   
    private SqlMapClient sqlMapClient;
    
    // </editor-fold>
    
    // <editor-fold desc="Constructors">
    
    /**
     * Provides data manager base initialization for given sql map config file
     * @param sqlMapConfig
     * @throws org.pg.biomedics.asr.data.DataAccessException
     */
    protected BaseDataManager(String sqlMapConfig) throws DataAccessException
    {
        try
        {            
            FileInputStream inputStream = new FileInputStream(sqlMapConfig);
            Reader reader = new InputStreamReader(inputStream);
            
            this.sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
            
            inputStream.close();
        }
        catch (Exception ex)
        {
            throw new DataAccessException("Can't initialize data manager", ex);
        }
    }
    
    /**
     * Provides data manager base initialization basing on configuration
     * retrived directly from properties
     * @throws org.pg.biomedics.asr.data.DataAccessException
     */
    protected BaseDataManager() throws DataAccessException
    {
        this(System.getProperty(SQL_MAP_CONFIG_KEY));
    }
    
    // </editor-fold>
    
    // <editor-fold desc="Properties">
    
    protected SqlMapClient getSqlMapClient()
    {
        return this.sqlMapClient;
    }
    
    // </editor-fold>
}
